package scct.zhang.utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DBUtils {
    // 数据库连接字符
    private static String Driver="com.mysql.cj.jdbc.Driver";
    private static String url =
            "jdbc:mysql://localhost:3306/storedb?characterEncoding=UTF-8&useSSL=false";
    private static String name="root";
    private static String pass = "123456";
    public static Connection getConnection(){
        Connection conn;
        try{
            Class.forName(Driver);
            conn= DriverManager.getConnection(url,name,pass);
        } catch (ClassNotFoundException e) {
           throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return conn;
    }

    public static void closeConnection(Connection conn){
        if(conn!=null){
            try{
                conn.close();
            }catch (SQLException e){
                throw new RuntimeException(e);
            }
        }
    }

    public static String getDriver() {
        return Driver;
    }

    public static void setDriver(String driver) {
        Driver = driver;
    }

    public static String getUrl() {
        return url;
    }

    public static void setUrl(String url) {
        DBUtils.url = url;
    }

    public static String getName() {
        return name;
    }

    public static void setName(String name) {
        DBUtils.name = name;
    }

    public static String getPass() {
        return pass;
    }

    public static void setPass(String pass) {
        DBUtils.pass = pass;
    }

    public DBUtils() {
    }
}
